10 ' conwaygw.bas - FreeWare 2005
20 GOTO 120 ' begin
30 SAVE "conwaygw.bas",A:LIST-120
40 GOTO 630  ' wait for key
50 GOTO 660  ' centered text
60 GOTO 680  ' line 1
70 GOTO 690  ' line 2
80 GOTO 720  ' line 3
90 GOTO 730  ' line 4
100 GOTO 760  ' reset zero
110 GOTO 1230 ' generate screen
120 SCREEN 9:CLS ' compile with screen 12 in QB or PB
130 DEFINT A-K,S-Z:DEFSTR M,Q:Q=MKI$(0):SH=120:SV=90
140 DIM U(SH,SV),V(SH,SV)
150 DATA 0,0,1,0,0,0,1,0,1,0,1,0,0,0,1,0,1,0,1,0,0,0,1,0,0
160 M(1)="GAME OF LIFE ideas by Hans Lauwerier"
170 M(2)="GW-BASIC by Eric Tchong"
180 M(3)="Press any key for this menu"
190 M(4)="<1> Traffic light in 8 steps"
200 M(5)="<2> Evolution of random cells"
210 M(6)="<3> Evolution of the R pentomino"
220 M(7)="<4> Glider gun in 150 generations"
230 M(8)="Choose 1..4   -1 = stop program "
240 FOR I=1 TO 8
250  GOSUB 50:IF I=3 OR I=7 THEN PRINT
260 NEXT
270 LOCATE 11,29:INPUT CH
280 IF CH=-1 THEN SCREEN 0,0,0:CLS:END
290 IF CH<1 OR CH>4 THEN 270
300 CLS:ON CH GOTO 320,830,950,1080
310 ' program 1
320 SH=20:SV=15:K=0:KMAX=8  ' was 16
330 WINDOW (3*SH-320,3*SV-240)-(3*SH+319,3*SV+239)
340 LINE (-4,-3)-(6*SH+4,6*SV+3),9,B:RESTORE
350 FOR J=SV/2+2 TO SV/2-2 STEP -1
360  FOR I=SH/2-2 TO SH/2+2
370   READ U(I,J)
380   IF U(I,J)=1 THEN GOSUB 60 ' line 1
390  NEXT
400 NEXT
410 WHILE K<KMAX
420  FOR I=1 TO SH-1
430   FOR J=1 TO SV-1
440    S0=U(I,J):V(I,J)=S0
450    S1=U(I-1,J-1)+U(I,J-1)+U(I+1,J-1)
460    S2=U(I-1,J)+U(I+1,J)
470    S3=U(I-1,J+1)+U(I,J+1)+U(I+1,J+1)
480    S=S1+S2+S3
490    IF S0=1 THEN IF S>3 OR S<2 THEN GOSUB 70:V(I,J)=0 ' line 2
500    IF S0=0 AND S=3 THEN GOSUB 60:V(I,J)=1 ' line 1
510   NEXT
520  NEXT
530  FOR I=1 TO SH-1
540   FOR J=1 TO SV-1
550    U(I,J)=V(I,J)
560    IF U(I,J)=1 THEN GOSUB 60 ' line 1
570   NEXT
580  NEXT
590  K=K+1:LOCATE 1,1:PRINT K:GOSUB 40
600 WEND:GOSUB 100 ' reset zero
610 CLS:GOTO 240
620 ' wait for key
630 LSET Q=MKI$(0)
640 WHILE CVI(Q)=0:MID$(Q,1)=INKEY$:WEND:RETURN
650 ' centered text
660 X=(80-LEN(M(I)))/2:PRINT TAB(X) M(I):RETURN
670 ' line 1
680 LINE (6*I-1,6*J-1)-(6*I+1,6*J+1),10,BF:RETURN
690 ' line 2
700 LINE (6*I-1,6*J-1)-(6*I+1,6*J+1),0 ,BF:RETURN
710 ' line 3
720 LINE (4*I-1,4*J-1)-(4*I+1,4*J+1),10,BF:RETURN
730 ' line 4
740 LINE (4*I-1,4*J-1)-(4*I+1,4*J+1),0 ,BF:RETURN
750 ' reset zero
760  FOR I=1 TO SH-1
770   FOR J=1 TO SV-1
780    IF U(I,J)=1 THEN U(I,J)=0
790   NEXT
800  NEXT
810 RETURN
820 ' program 2
830 RANDOMIZE 11:PROC=20:SH=120:SV=90
840 K=1:KMAX=2000:P=PROC/100
850 WINDOW (2*SH-320,2*SV-240)-(2*SH+319,2*SV+239)
860 LINE (-4,-3)-(4*SH+4,4*SV+3),9,B
870 FOR I=1 TO SH-1
880  FOR J=1 TO SV-1
890   R=RND
900   IF R<P THEN U(I,J)=1:GOSUB 80 ' line 3
910  NEXT
920 NEXT
930 GOSUB 110:GOSUB 100:GOSUB 40:CLS:GOTO 240
940 ' program 3
950 SH=120:SV=90:K=0:KMAX=1200
960 WINDOW (2*SH-320,2*SV-240)-(2*SH+319,2*SV+239)
970 RESTORE 980
980 DATA 0,1,1,1,1,0,0,1,0
990 LINE (-4,-3)-(4*SH+4,4*SV+3),9,B
1000 FOR J=SV/2+1 TO SV/2-1 STEP -1
1010  FOR I=SH/2-1 TO SH/2+1
1020   READ U(I,J)
1030   IF U(I,J)=1 THEN GOSUB 80 ' line 3
1040  NEXT
1050 NEXT
1060 GOSUB 110:GOSUB 100:GOSUB 40:CLS:GOTO 240
1070 ' program 4
1080 SH=60:SV=50:K=0:KMAX=150
1090 WINDOW (2*SH-320,2*SV-240)-(2*SH+319,2*SV+239)
1100 LINE (-4,-3)-(4*SH+4,4*SV+3),9,B
1110 DATA 0,3, 0,4, 1,3, 1,4, 11,2,11,3,11,4,12,1,12,5
1120 DATA 13,0,13,6,14,1,14,5,15,2,15,3,15,4,16,2,16,3
1130 DATA 16,4,21,4,21,5,21,6,22,3,22,4,22,6,22,7,23,3
1140 DATA 23,4,23,6,23,7,24,3,24,4,24,6,24,7,25,2,25,3
1150 DATA 25,7,25,8,26,5,30,2,30,3,34,4,34,5,35,4,35,5
1160 RESTORE 1110
1170 FOR I=1 TO 45
1180  READ X,Y:X=X+3:Y=Y+36:U(X,Y)=1
1190  LINE (4*X-1,4*Y-1)-(4*X+1,4*Y+1),10,BF ' x y
1200 NEXT
1210 GOSUB 110:GOSUB 100:GOSUB 40:CLS:GOTO 240
1220 ' generate screen
1230 WHILE K<=KMAX AND INKEY$=""
1240  LOCATE 1,1:PRINT K
1250  FOR I=1 TO SH-1
1260   FOR J=1 TO SV-1
1270    S0=U(I,J):V(I,J)=S0
1280    S1=U(I-1,J-1)+U(I,J-1)+U(I+1,J-1)
1290    S2=U(I-1,J)+U(I+1,J)
1300    S3=U(I-1,J+1)+U(I,J+1)+U(I+1,J+1)
1310    S=S1+S2+S3
1320    IF S0=1 THEN IF S>3 OR S<2 THEN GOSUB 90:V(I,J)=0 ' line 4
1330    IF S0=0 AND S=3 THEN GOSUB 80:V(I,J)=1 ' line 3
1340   NEXT
1350  NEXT
1360  FOR I=1 TO SH-1
1370   FOR J=1 TO SV-1
1380    U(I,J)=V(I,J)
1390    IF U(I,J)=1 THEN GOSUB 80 ' line 3
1400   NEXT
1410  NEXT:K=K+1
1420 WEND:RETURN
